package mapper

import (
	"gin-demo/models"
	bizcode "gin-demo/utils/bizerror"
)

type CaseLabelLogMapper struct {
	BaseMapper
}

type listFilter struct {
	Page         uint64
	Limit        uint64
	SerialNumber string
	CaseId       int
	LabelId      string
	LabelName    string
	UserId       string
	StartTime    int
	StopTime     int
}

type ListFilter listFilter

func (caseLogMapper *CaseLabelLogMapper) GetByCondition(filter *ListFilter, field []string) ([]models.CaseLabelLog, uint64) {
	var whereOrder []models.PageWhereOrder
	if filter.SerialNumber != "" {
		whereOrder = append(whereOrder, *caseLogMapper.WhereEq(filter.SerialNumber, "serial_number"))
	}
	if filter.CaseId != 0 {
		whereOrder = append(whereOrder, *caseLogMapper.WhereEq(filter.CaseId, "case_id"))
	}
	if filter.LabelId != "" {
		whereOrder = append(whereOrder, *caseLogMapper.WhereEq(filter.LabelId, "case_label_id"))
	}
	if filter.LabelName != "" {
		whereOrder = append(whereOrder, *caseLogMapper.WhereLike(filter.LabelName, "case_label_name"))
	}
	if filter.UserId != "" {
		whereOrder = append(whereOrder, *caseLogMapper.WhereEq(filter.UserId, "user_id"))
	}
	if filter.StartTime > 0 {
		whereOrder = append(whereOrder, *caseLogMapper.WhereEGT(filter.StartTime, "add_time"))
	}
	if filter.StopTime > 0 {
		whereOrder = append(whereOrder, *caseLogMapper.WhereELT(filter.StopTime, "add_time"))
	}
	var total uint64
	var list []models.CaseLabelLog
	err := models.GetPage(&models.CaseLabelLog{}, &models.CaseLabelLog{}, &list, field, filter.Page, filter.Limit, &total, whereOrder...)
	bizcode.Check(err)

	return list, total
}
